package method;

import java.util.Scanner;

public class testCopyOfRange {
    //计算num1的下标
    public static int indexFirst(int[] arr, int num1) {
        int index1;
        for (index1 = 0; index1 < arr.length; index1++) {
            if (num1 == arr[index1])
                return index1;
        }
        //若无此数，返回-1
        return -1;
    }

    //计算num2的下标
    public static int indexSecond(int[] arr, int index1, int num2) {
        int index2;
        for (index2 = index1 + 1; index2 < arr.length; index2++) {
            if (num2 == arr[index2])
                return index2;
        }
        //若无此数，返回-1
        return -1;
    }

    //生成arr2的数据，并作为函数返回值
    public static int[] resultArr(int[] arr, int index1, int index2, int length) {
        int[] resultArr = new int[length];
        int count = 0;
        for (int i = index1; i < index2 + 1; i++) {
            resultArr[count] = arr[i];
            count++;
        }
        return resultArr;
    }

    public static void main(String[] args) {
        int[] arr = {9, 2, 6, 3, 8, 2, 4, 2, 7, 5, 3};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入两个整数");
        int num1 = sc.nextInt(), num2 = sc.nextInt(), index1 = indexFirst(arr, num1);
        //排除异常
        if (index1 != -1) {
            int index2 = indexSecond(arr, index1, num2);
            //排除异常
            if (index2 != -1) {
                //计算数组长度
                int length = index2 - index1 + 1;
                int[] ResultArr = resultArr(arr, index1, index2, length);
                for (int i = 0; i < length; i++)
                    System.out.print(ResultArr[i]);
            } else
                System.out.println("数组中" + num1 + "后不存在数字" + num2 + "！");
        } else
            System.out.println("数组中不存在此数！");
    }
}